<?php
/**
 * This is elements/admin_sidebar template file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.views.elements
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
	if (!empty($admin_sidebar) && $this->Lil->user('id')) {
		foreach ($admin_sidebar as $panel_name => $panel) {
			if (!empty($panel['visible']) && !empty($panel['items'])) {
				
				echo '<div class="panel">' . PHP_EOL;
				foreach ($panel['items'] as $li_name => $li) {
					if (!empty($li['visible'])) {
						echo '<div>';
						echo '<h1' . ($li['active'] ? ' class="active"' : '') . '>' . PHP_EOL;
						
						if ( !empty($li['submenu'])) {
							echo '<div class="expand' . ($li['active'] ? ' expanded' : '') . '" onclick="expandMenu(this);"></div>' . PHP_EOL;
						}
						
						echo $this->Html->link(
							$li['title'],
							$li['url'],
							(isset($li['params']) ? $li['params'] : null)
						) . PHP_EOL;

						echo '</h1>' . PHP_EOL;
						if ( !empty($li['submenu'])) {
							printf('<ul%s>' . PHP_EOL, (empty($li['expand']) ? ' class="folded"' : ' class="expanded"'));
							foreach ($li['submenu'] as $lis) {
								if (!empty($lis['visible'])) {
									echo '<li' . ($lis['active'] ? ' class="active"' : '') . '>';
									echo $this->Html->link(
										$lis['title'],
										$lis['url'],
										(isset($lis['params']) ? $lis['params'] : null)
									);
									echo '</li>' . PHP_EOL;
								}
							}
							echo '</ul>' . PHP_EOL;
						}
						echo '</div>';
					}
				}
				echo '</div>' . PHP_EOL;
			}
		}
		
						
		echo '<script type="text/javascript">' . PHP_EOL;
		echo '	function expandMenu(node) {' . PHP_EOL;
		echo '		var children = node.parentNode.parentNode.childNodes;' . PHP_EOL;
		echo '		for (var i=0; i < children.length; i++) {' . PHP_EOL;
		echo '			if (children[i].tagName == "UL") {' . PHP_EOL;
		echo '				if (children[i].className.match(new RegExp(\'(\\s|^)folded(\\s|$)\'))) {' . PHP_EOL;
		echo '					var reg = new RegExp(\'(\\s|^)folded(\\s|$)\');' . PHP_EOL;
		echo '					children[i].className = children[i].className.replace(reg, \'expanded\');' . PHP_EOL;
		echo '					node.className += \' expanded\';' . PHP_EOL;
		echo '				} else {' . PHP_EOL;
		echo '					var reg = new RegExp(\'(\\s|^)expanded(\\s|$)\');' . PHP_EOL;
		echo '					children[i].className = children[i].className.replace(reg, \'folded\');' . PHP_EOL;
		echo '					node.className = \'expand\';' . PHP_EOL;
		echo '				}' . PHP_EOL;
		echo '			}' . PHP_EOL;
		echo '		}' . PHP_EOL;
		echo '	}' . PHP_EOL;
		echo '</script>';
	}
?>